<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    var $article;
    var $about;
    var $category;
    var $customer;

    function __construct()
    {
        parent::__construct();
        $this->article = M('Article');
        $this->about = M('About');
        $this->category = M('Category');
        $this->customer = M('Customer');
    }

    /***
     * 前端首页
     */
    public function index()
    {
//        $articles = $this->article->order('sort_order desc')->select();
//        $category = $this->category->order('sort_order desc')->select();
//        foreach($articles as $k=>$v){
//            $article[$k]['category'] = $this->category->where('id = $v[category_id]')->find();
//        }
        $Article = M('Article');
        $Category = M('Category');
        $articles = $this->article->order('sort_order desc')->select();
        foreach ($articles as $key => $value) {
            $articles[$key]['category'] = $Category->where("id = $value[category_id]")->find();
        }

        $this->assign('articles', $articles);
        $this->display();
    }

    /***
     * 加载--关于我
     */
    public function about()
    {
//        $About = M('About')->find();
        $about = $this->about->find();
        $this->assign('about', $about);
        $this->display();
    }

    /***
     * 加载--注册页面
     */
    public function register()
    {
        $Customer = M('Customer');
        if (IS_POST) {
            $data['username'] = trim(I('post.username'));
            if ($data['username'] == '') {
                $this->error("请填写用户名");
            }
            //查询数据库里面是否存在此用户
            $customer = $Customer->where($data)->find();
            if ($customer) {
                $this->error('此用户名已被注册');
            }
            if (strlen($data['username']) < 5 || strlen($data['username']) > 20) {
                $this->error("用户名长度不能小于5位并且不能大于20位");
            }
            $data['password'] = set_password(I('post.password'));
            if ($data['password'] == '') {
                $this->error("请填写密码");
            }

            $data['thumb'] = I('post.thumb');

            /***
             * 验证 两次密码是否一致
             */
            $check_password = I('post.password');
            if (!$check_password = I('post.password')) {
                $this->error("两次输入密码不一致");
            }
            $Customer->add($data);
            $this->success("恭喜你，注册成功", U('Index/login'));
        } else {
            $this->display();
        }
    }

    /***
     * 加载登录页面
     */
    public function login()
    {
        $Customer = M('Customer');
        if (IS_POST) {
            $data['username'] = trim(I('post.username'));
            $data['password'] = set_password(I('post.password'));

            $code = I('post.code');
            if ($code == '') {
                $this->error('请填写验证码');
            }
            if (!$this->check_verify($code)) {
                $this->error('验证码填写错误');
            }

            //查询数据库里面是否存在此用户
            $customer = $Customer->where($data)->find();
            if (!$customer) {
                $this->error('用户名或密码输入错误');
            }
            $_SESSION['user'] = $customer;
            $this->success('恭喜您，登录成功', U('Index/index'));
        } else {
            $this->display();
        }
    }

    /***
     * 验证码
     */
    public function verify()
    {
        $config = array(
            'fontSize' => 30,    // 验证码字体大小
            'length' => 4,     // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
            'codeSet' => '0123456789'
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }



    public function show()
    {
        $id = I('get.id');
        $article = $this->article->find($id);
        $this->assign('article', $article);
        $this->display();
    }


}
